package com.example.t410.service.serviceImpl;

import com.example.t410.VO.AnswerVO;
import com.example.t410.VO.QuestionVO;
import com.example.t410.VO.QuestionVOPlus;
import com.example.t410.entity.Modules;
import com.example.t410.entity.Questions;
import com.example.t410.mapper.ModulesMapper;
import com.example.t410.mapper.QuestionsMapper;
import com.example.t410.service.TestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepressionServiceImpl implements TestService {
    @Autowired
    QuestionsMapper questionsMapper;
    @Autowired
    private ModulesMapper modulesMapper;
    @Override
    public QuestionVOPlus findAll(Long id) {
        Modules modules = modulesMapper.getById(id);
        List<Questions> questionsList =  questionsMapper.getByTestId(id);
        List<QuestionVO> questionVOS = new ArrayList<>();
        for (Questions questions:questionsList) {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(questions, questionVO);
            String[] options = questions.getOptions().split(" ");
            String[] vals = questions.getVal().split(" ");
            questionVO.setOptions(options);
            questionVO.setVal(vals);
            questionVOS.add(questionVO);
        }
        QuestionVOPlus questionVOPlus = new QuestionVOPlus();
        questionVOPlus.setQuestionVOList(questionVOS);
        questionVOPlus.setQuestionType(modules.getType());
        return questionVOPlus;
    }

    public AnswerVO calculateAns(String ans) {
        char[] chars = ans.toCharArray();
        if(ans.length()!=20) try {
            throw new Exception();
        } catch (Exception e) {
            System.out.println("输入长度错误！！");
        }
        List<Questions> questionsList =  questionsMapper.getByTestIdAndReverse((long)3);//抑郁列表

        int[] reverseIndex = new int[questionsList.size()];
        for (int i = 0; i < reverseIndex.length; i++) {
            reverseIndex[i]=questionsList.get(i).getQuestionNumber();
        }

        int j =0;
        int sum = 0;
        for (int i = 0; i < chars.length; i++) {
            int temp = chars[i] - '0';
            if (i == reverseIndex[j]-1)
            {
                temp =5-temp;
                j++;
            }
            sum+=temp;
        }
        int total = (int) Math.round(sum*1.25);
        AnswerVO answerVo = new AnswerVO();
        answerVo.setTotal(total);
        if(total<=25)
        {
            answerVo.setLevel("正常");
            answerVo.setAdvice("您最近没有抑郁情绪，心理状态良好，继续探索本产品吧！");
            answerVo.setDescription("无");
        } else if (total<=60) {
            answerVo.setLevel("轻度抑郁");
            answerVo.setAdvice("可通过音乐冥想放松心情，太极拳等运动释放压力");
            answerVo.setDescription("早晨心情沉重、体重减轻、头脑不清楚、感到自己无用。您有时：" +
                    "饭量下降、感到做事困难、觉得未来没有希望、觉得难以下决定、生活没有意义、" +
                    "不喜爱自己平时喜爱的东西。您偶尔：感到情绪沮丧、郁闷、要哭或想哭、便秘、" +
                    "感到疲劳。");
        }else if (total<=62) {
            answerVo.setLevel("中度抑郁");
            answerVo.setAdvice("可先进行AI陪聊稳定情绪，知识科普模块了解自身情况，朋辈互助模块发帖寻求帮助等如仍有不适，前往线下心理咨询中心就医（在本产品的心理咨询中心模块可查看心理咨询中心相关信息）");
            answerVo.setDescription("感到情绪沮丧、郁闷、要哭或想哭、夜间睡眠不好、体重减轻、便" +
                    "秘、感到心跳加快、感到疲劳、坐卧不安、容易激怒、想到死。");
        } else {
            answerVo.setLevel("重度抑郁");
            answerVo.setAdvice("请及时前往线下心理咨询中心就医（在本产品的心理咨询中心模块可查看心理咨询中心相关信息）");
            answerVo.setDescription("感到情绪沮丧、郁闷、早晨心情沉重、要哭或想哭、夜间睡眠不好、" +
                    "饭量下降、性功能不正常、体重减轻、便秘、感到心跳加快、感到疲劳、头脑不" +
                    "清楚、感到做事困难、坐卧不安、觉得未来没有希望、容易激怒、觉得难以下决" +
                    "定、感到自己无用、生活没有意义、想到死、不喜爱自己平时喜爱的东西");
        }
        return answerVo;
    }
}
